package com.qianfeng.iot.sdk.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.dc3.common.bean.driver.AttributeInfo;
import com.dc3.common.bean.point.PointValue;
import com.dc3.common.model.Device;
import com.qianfeng.iot.sdk.bean.driver.DriverContext;
import com.qianfeng.iot.sdk.service.DriverCommandService;
import com.qianfeng.iot.sdk.service.DriverCustomService;
import com.qianfeng.iot.sdk.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jackiechan on 2022/8/11 21:45
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */

@Service
public class DriverCommandServiceImpl implements DriverCommandService {


    private DriverContext driverContex;


    private DriverCustomService driverCustomService;

    private DriverService driverService;


    @Autowired
    public void setDriverService(DriverService driverService) {
        this.driverService = driverService;
    }

    @Autowired
    public void setDriverCustomService(DriverCustomService driverCustomService) {
        this.driverCustomService = driverCustomService;
    }

    @Autowired
    public void setDriverContex(DriverContext driverContex) {
        this.driverContex = driverContex;
    }

    @Override
    public PointValue read(String deviceId, String pointId) {
        //首先根据设备的id找到设备,也就是当前连接到驱动的设备
        Device device = driverContex.getDriverMetadata().getDeviceMap().get(deviceId);
        //从设备中找数据,但是这个数据到底是什么数据其实是由我们具体的驱动来决定,我们在这边并不知道,此处应该交给用户具体处理
        String rawString = driverCustomService.read(driverContex.getDriverInfoByDeviceId(deviceId), driverContex.getPointInfoByDeviceIdAndPointId(deviceId, pointId), device, driverContex.getPointByDeviceIdAndPointId(deviceId, pointId));
        //根据用户自己的功能实现的数据,然后封装到PointValue返回
        return new PointValue(deviceId,pointId,rawString,driverService.convertValue(deviceId,pointId,rawString));
    }

    @Override
    public Boolean write(String deviceId, String pointId, String value) {
        //获取到设备
        Device device = driverContex.getDriverMetadata().getDeviceMap().get(deviceId);
        //针对这个设备我们要怎么发送数据, 因为还有指定的位号
       // return driverCustomService.write(driverContex.getDriverInfoByDeviceId(deviceId), driverContex.getPointInfoByDeviceIdAndPointId(deviceId, pointId), device, new AttributeInfo(value, driverContex.getPointByDeviceIdAndPointId(deviceId, pointId).getType()));
        // 因为没有发送数据过来,我们所以写了一个假的数据,实际上会自动根据设备绑定的位号获取对应的值,我们这边没有同步过来
        Map<String, AttributeInfo> driverInfo = new HashMap<>();
        Map<String, AttributeInfo> pointInfo = new HashMap<>();
        pointInfo.put("commandTopic", new AttributeInfo("commandTopic", "string"));
        pointInfo.put("commandQos", new AttributeInfo("2", "int"));
        return driverCustomService .write(driverInfo, pointInfo, device, new AttributeInfo(value, driverContex.getPointByDeviceIdAndPointId(deviceId, pointId).getType()));
    }
}
